<?php

namespace App\Http\Controllers\Api;

use App\Http\Model\ResponseResult;
use App\Models\User;
use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{

    public function __construct()
    {
        $this->middleware('api-auth');
        $this->middleware('permission:admin-user')->except('index');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = \Auth::user();
        foreach ($user->roles as $role) {
            $role->perms;
        }
        return response(ResponseResult::success($user));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function store(Request $request)
    {
        $this->validator($request, [
            'name' => 'required|max:32',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:6|max:32',
            'avatar' => 'required|max:255'
        ]);
        $data = $request->only(['name', 'email', 'password', 'avatar']);
        $data['password'] = bcrypt($data['password']);
        $user = User::create($data);
        if (!empty($user)) {
            return ResponseResult::success(__('app.create.success'));
        }
        return ResponseResult::error(1000, __('app.create.fail'));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\User $user
     * @return array
     */
    public function show(User $user)
    {
        $user->roles;
        return ResponseResult::success($user);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\User $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\Models\User $user
     * @return array
     */
    public function update(Request $request, User $user)
    {
        if (Auth::user()->checkUserRoleFlag($user) === false) {
            return ResponseResult::error(1000, __('app.update.not'));
        }
        $this->validator($request, [
            'name' => 'max:32',
            'new_password' => 'min:6|max:32',
            'avatar' => 'max:255',
            'roles_id' => 'exists:roles,id'
        ]);
        $data = $request->only(['name', 'new_password', 'avatar']);
        if (isset($data['new_password'])) {
            $data['password'] = bcrypt($data['new_password']);
            unset($data['new_password']);
        }
        if ($user->update($data)) {
            if ($request->has('roles_id')) {
                $user->detachRoles($user->roles);
                $user->save();
                $user->attachRole($request->get('roles_id'));
                $user->save();
            }
            return ResponseResult::success(__('app.update.success'));
        }
        return ResponseResult::error(2000, __('app.update.fail'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\User $user
     * @return array
     */
    public function destroy(User $user)
    {
        if (Auth::user()->checkUserRoleFlag($user) === false) {
            return ResponseResult::error(1000, __('app.delete.not'));
        }

        if ($user->delete()) {
            return ResponseResult::success(__('app.delete.success'));
        };
        return ResponseResult::error(2000, __('app.delete.fail'));
    }

    /**
     * 用户列表
     *
     * @param Request $request
     * @return array
     */
    public function list(Request $request)
    {
        if ($request->has('q')) {
            $q = $request->get('q');
            $users = User::where('name', 'like', "%$q%")->orderBy('id')->paginate(20);
        } else {
            $users = User::orderBy('id')->paginate(20);
        }
        return ResponseResult::success($users);
    }
}
